# FILE: DurationFactory.py
# DESC: Factory implementation for Duration objects.

from EntityFactory import EntityFactory
from web.entity.Duration import Duration
from infra.logging import logger

import re

class DurationFactory(EntityFactory):

	def __init__(self, dataFilters):
		EntityFactory.__init__(self)
		self.dataFilters_ = dataFilters
		self.data_ = None 


	def validate(self, argv):
		log = logger.getLogger()
		rv = True
		data = argv

		for dataFilter in self.dataFilters_:
			data = dataFilter.filter(data)

		m = re.search(self.getEntityType(), data)
		if m:
			data = m.group(0)

		if not data:
			self.errMsg_ = "All data filtered out. DurationFactory has nothing to work with."
			log.warning(self.errMsg_)
			rv = False
		else:
			self.data_ = data

		return rv 


	def doCreateEntity(self, consumer, relevanceFactor):
		duration = Duration(self.data_)
		if consumer:
			consumer.onDuration(duration)
		return duration


	def getEntityType(self):
		return ('^[0-9]+\s*(((D|d)ay(s)?)|((W|w)eek(s)?))$')
